
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var studentsRouter = require('./routes/students')
var usersRouter = require('./routes/users');
var todoRouter = require('./routes/todos');
var diaryRouter = require('./routes/diary');
var timeArrowRouter=require('./routes/timeArrow');
var lifeCycleRouter=require('./routes/lifeCycle');
var exerciseRouter=require('./routes/exercise');
var personRouter=require('./routes/person');
var quoteRouter=require('./routes/quote');
var titleRouter=require('./routes/title');
var paragraphRouter=require('./routes/paragraph');
var hobbyRouter=require('./routes/hobby')



var app = express();
//引入session
const session=require('express-session');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser('VERIFYCODE'));//使用VERIFYCODE进行加密
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'assets')));

// cros跨域
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Credentials", "true")//允许前端携带认证如cookie
    res.header("Access-Control-Allow-Origin", "*")//允许所有源访问
    res.header("Access-Control-Allow-Headers", "authorization,Content-Type");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
// 使用session
// app.use(session({
//     secret:'ZI_MA_KAI_MEN',//加密的字符串，内容可随便写
//     resave:false,//强制保存session,即使它没变化
//     saveUninitialized:true,//强制将未初始化的session存储，默认为true
//     cookie:{maxAge:1000*60*60}//过期时间1h
// }))
// 定义一个拦截器，用于验证前端发送的请求
// const interceptores = (req, res, next) => {
//     if(req.path==='/users/login'||req.path==='/users/register'){
//         next()
//     }else{
//         let {userInfo}=req.session;
//         console.log(userInfo,999999999)
//         if(userInfo){//已登入
//             //预留一个接口用于验证是否登入
//             if(req.path==='/verify'){
//                 res.send({status:200,state:true,msg:'已登入',data:userInfo})
//             }else{
//                 next();
//             }
//         }else{
//             res.send({status:400,state:false,msg:'验证失败，请登入'})
//         }
//     }
// }

// app.use(interceptores);
app.use('/students', studentsRouter);
app.use('/users', usersRouter);
app.use('/plan', todoRouter);
app.use('/diary', diaryRouter);
app.use('/timeArrow', timeArrowRouter);
app.use('/lifeCycle', lifeCycleRouter);
app.use('/exercise', exerciseRouter);
app.use('/person',personRouter);
app.use('/quote',quoteRouter);
app.use('/title',titleRouter);
app.use('/paragraph',paragraphRouter);
app.use('/hobby',hobbyRouter);



module.exports = app;
